<?php

/**
* Calendar Helper for CakePHP
*
*	Copyright 2008 John Elliott
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
*
* @author John Elliott
* @copyright 2008 John Elliott
* @link http://www.flipflops.org More Information
* @license			http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

class CalendarHelper extends Helper
{

	var $helpers = array('Html', 'Form');
   
/**
* Generates a Calendar for the specified by the month and year params and populates it with the content of the data array
*
* @param $year string
* @param $month string
* @param $data array
* @param $base_url
* @return string HTML code to display calendar in view
*
*/
	
	function calendar($year = '', $month = '', $user = '', $data = '', $base_url ='', $totales='', $aprobado){
		
			$str = '';		
			$month_list = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
			$month_list2 = array('enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre');
			$numbers_month = array('enero'=>1, 'febrero'=>2, 'marzo'=>3, 'abril'=>4, 'mayo'=>5, 'junio'=>6,
								 'julio'=>7, 'agosto'=>8, 'septiembre'=>9, 'octubre'=>10, 'noviembre'=>11, 'diciembre'=>12);
			$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
			$day_list2 = array('Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab', 'Dom');
			$this->Html->link ($day = 1);
			$today = 0;
			

			$flag = 0;
				
			for($i = 0; $i < 12; $i++){			
					if(strtolower($month) == $month_list2[$i]){		
						if(intval($year) != 0){
								$flag = 1;
								$month_list = $i + 1;
								break;
						}
					}
			}
			
			if($year == date('Y') && strtolower($month) == strtolower(strftime('%B',strtotime(date('F'))))){	
					$today = date('j');
			}
			
			$number_month = $numbers_month[$month];
			$days_in_month = date("t", mktime(0,0,0, $number_month , 1, $year));
		    $first_day_in_month = date('D', mktime(0,0,0, $number_month , 1, $year));

			$str .= '<table class="calendar">';
			$str .= '<thead>';
			$str .= '<tr><th class="cell-prev">';
			$str .= '</th><th colspan="5">' . ucfirst($month) . ' ' . $year . '</th><th class="cell-next">';
			$enlace1 =  $this->Html->link($this->Html->image('add_actividad.png', array('alt'=> __('Nueva Actividad', true), 'border' => '0')),
					'../planeaciones/add/'.$number_month,array('escape' => false));
			$str .= $enlace1.'</th></tr>';
			$str .= '<tr>';
			for($i = 0; $i < 7;$i++) {
				$str .= '<th class="cell-header">' . $day_list2[$i] . '</th>';
			}
			$str .= '</tr>';
			$str .= '</thead>';
			$str .= '<tbody>';

		    
			while($day <= $days_in_month){

				$str .= '<tr>';
				for($i = 0; $i < 7; $i ++){						
					$cell = '&nbsp;';
 					$total = 0;
					if(isset($data[$number_month]['dia_'.$day])){					
							$cell = $data[$number_month ][$day];
							$total = ($aprobado)?$totales[$number_month][$day]['sin_aprobar']:$totales[$number_month][$day]['sin_aprobar']+$totales[$number_month][$day]['aprobados'];
					}
	
					$class = '';
					if($i > 4){
						$class = ' class="cell-weekend" ';
					}

					if($day == $today){
						$class = ' class="cell-today" ';
					}
					if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day <= $days_in_month)){

						
						$str .= '<td ' . $class . '><div class="cell-number">' .$day .'-'.$total. '</div><div class="cell-data">' . $cell . '</div></td>';

						$day++;
					}
					else{
						$str .= '<td ' . $class . '>&nbsp;</td>';
					}
				}
			
				$str .= '</tr>';
			}

			$str .= '</tbody>';			
			$str .= '</table>';
			
			return $str;
		}
}
?>
